########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""
RE - class to handle regular expression variables
"""
import re

class RE(object):
	"""A regular-expression object
	"""
	def __init__(self,s):
		if isinstance(s,RE):
			self.s=s.s
		elif 'match' in dir(s):
			self.s=s
		elif isinstance(s,str):
			self.s=re.compile(s,re.I)
		else:
			raise Exception('Can''t create RE object from '+str(s))
	def __repr__(self): return '/'+self.s.pattern+'/'
	def __str__(self): return `self`
	def __eq__(self,other):
		"""An RE object is equal to a string if searching for the regular-expression somewhere in the string yields a match"""
		if isinstance(other,RE):
			return self.s.pattern == other.s.pattern
		else:
			return bool(self.s.search(str(other)))
	def __neq__(self,other): return not self==other

if __name__=='__main__':
	r=RE('hello')
	print r=='hello'
	print r in ['held', 'there']
	print r=='hellot'
	print 'hellot'==r
